from flask_script import Manager
from info import create_app, db
from flask_migrate import Migrate, MigrateCommand
from info.models import User


# 创建运行对象
app = create_app('develop')
manager = Manager(app)
Migrate(app, db)
manager.add_command('db', MigrateCommand)


@manager.option('-u', '-user_name', dest='user_name')
@manager.option('-p', '-password', dest='password')
def create_admin(user_name, password):
    """创建管理员账号  那个dest并不是提示写什么  而是往里面传递参数的时候 对应的是什么参数"""
    user = User()
    if not all([user_name, password]):
        print('参数不足')
        return

    user.mobile = user_name
    user.nick_name = user_name
    user.password = password
    user.is_admin = True

    try:
        db.session.add(user)
        db.session.commit()
        print('创建成功')
    except Exception as e:
        print(e)
        db.session.rollback()

if __name__ == '__main__':
    manager.run()
